const db = require('../db/index')
const utils = require('../utils/index')


module.exports = {
   async reqGetDitor(req, res) {
        let sql = `SELECT id,val,update_time AS updateTime,create_time AS createTime FROM ditor WHERE 1=1`, obj = req.body;
        let { total } = await utils.getSum({ sql, name: "ditor", res, req });
        sql += ` ORDER BY id asc`;
        sql = utils.pageSize(sql, obj.pageNum, obj.pageSize);
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            res.send({ data: { list: results, pageNum: obj.pageNum, pageSize: obj.pageSize, total: obj.username ? results.length : total } })
        })
    },
    reqAddDitor(req,res){
        let sql = "INSERT INTO ditor(val) VALUES (?)",
        obj = req.body;
        db.query(sql,[obj.val],(err,results)=>{
            if (err) return res.cc(err)
            res.send({code:200,msg:'添加成功'})
        })
    },
    reqEditDitor(req,res){
        let sql = "UPDATE  ditor SET val=? WHERE id=?",
        obj = req.body;
        db.query(sql,[obj.val,obj.id],(err,results)=>{
            if (err) return res.cc(err)
            res.send({code:200,msg:'修改成功'})
        })
    },
    reqDelDitor(req,res){
        let sql = `DELETE FROM ditor WHERE id IN (?)`;
        obj = req.body;
        db.query(sql,[obj.id],(err,results)=>{
            if (err) return res.cc(err)
            res.send({code:200,msg:'删除成功'})
        })
    }
}